package mae.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class SuperheroAdapter {
	public static final String KEY_ROWID = " _id";
	public static final String KEY_NAME = "Name";
	public static final String KEY_YEAR = "Year";
	public static final String KEY_VALUE = "Value";
	public static final String KEY_STATUS = "Status";
	private static final String SUPERHERO_TABLE = "superhero";
	private Context context;
	private SQLiteDatabase db;
	private CollectionsDatabaseHelper dbHelper;

	public SuperheroAdapter(Context context) {
		this.context = context;
	}

	public void open(){
		dbHelper.createDB();
		dbHelper.openDatabase();
		
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Update the Superhero item
	 */
	public boolean updateSuperhero(long rowId, String status) {
		ContentValues values = createContentValues(status);
		return db
				.update(SUPERHERO_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over all superhero
	 * 
	 * @return Cursor for all superhero
	 * 
	 */
	public Cursor fetchAllSuperhero() {
		return db
				.query(SUPERHERO_TABLE, new String[] { KEY_ROWID, KEY_NAME,
						KEY_YEAR, KEY_VALUE, KEY_STATUS }, null, null, null,
						null, null);
	}

	/**
	 * Return a Cursor positioned at the defined Superhero
	 */
	public Cursor fetchSuperhero(long rowId) throws SQLException {
		Cursor mCursor = db.query(SUPERHERO_TABLE, new String[] { KEY_NAME,
				KEY_YEAR, KEY_VALUE, KEY_STATUS }, KEY_ROWID + "=" + rowId,
				null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/**
	 * Puts the inputed value into the database
	 * 
	 * @param status
	 * @return
	 */
	private ContentValues createContentValues(String status) {
		ContentValues values = new ContentValues();
		values.put(KEY_VALUE, status);
		return values;
	}
}
